package com.icss.day05;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class Test30 {
    public static void main(String[] args) {
        Student stu = new Student(1, "tom1", 9);
        Student stu2 = new Student(2, "tom2", 12);
        Student stu3 = new Student(3, "tom3", 21);

        // 验证 对象是否合法
        try {
            check1(stu2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void check1(Student stu) throws Exception {

        // 反射 获取类对象
        Class clazz = stu.getClass();

        // 遍历所有属性
        for (Field field: clazz.getDeclaredFields()) {

            for (Annotation annotation: field.getDeclaredAnnotations()) {
                if (annotation instanceof Check) {
                    // 获取注解对象
                    Check check = (Check)annotation;
                    int min = check.min();
                    int max = check.max();
                    String message = check.value();

                    if (stu.getAge() < min  || max < stu.getAge()) {
                        throw new Exception(message);
                    }

                }
            }
        }

    }
}
